<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class StaffAuth extends Model
{
    protected $table = 'ims_hhm_staff_auth';
    
    protected $primaryKey = 'id';
    
    public $incrementing = true;
    
    public $timestamps = false;
    
    protected $fillable = [
        'uid',
        'store_id',
        'status',
        'operate_id',
        'created_at',
        'updated_at',
        'deleted_at'
    ];
    
    protected $casts = [
        'id' => 'integer',
        'uid' => 'integer',
        'store_id' => 'integer',
        'status' => 'integer',
        'operate_id' => 'integer',
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'deleted_at' => 'integer'
    ];
    
    // 状态常量
    const STATUS_AUTHORIZED = 10; // 已授权
    const STATUS_CANCELLED = 20;  // 取消授权
    
    /**
     * 获取状态文本
     */
    public function getStatusTextAttribute()
    {
        $statusMap = [
            self::STATUS_AUTHORIZED => '已授权',
            self::STATUS_CANCELLED => '取消授权'
        ];
        
        return $statusMap[$this->status] ?? '未知状态';
    }
    
    /**
     * 关联会员信息
     */
    public function member()
    {
        return $this->belongsTo(Member::class, 'uid', 'uid');
    }
    
    /**
     * 关联操作人信息
     */
    public function operator()
    {
        return $this->belongsTo(Member::class, 'operate_id', 'uid');
    }
    
    /**
     * 查询已授权的员工
     */
    public function scopeAuthorized($query)
    {
        return $query->where('status', self::STATUS_AUTHORIZED);
    }
    
    /**
     * 查询未删除的记录
     */
    public function scopeNotDeleted($query)
    {
        return $query->whereNull('deleted_at');
    }
    
    /**
     * 按门店ID查询
     */
    public function scopeByStore($query, $storeId)
    {
        return $query->where('store_id', $storeId);
    }
}
